@extends('admin.layout')
@section('title',$title)
@section('content')
    <meta name="_token" content="{{ csrf_token() }}"/>
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                商品列表
                <small>advanced</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="/admin/goods/show"><i class="fa fa-dashboard"></i>商品列表 </a></li>
                </li>
            </ol>
        </section>
        @if (session('success'))
            <div class="alert alert-success alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <h4><i class="icon fa fa-check"></i> 成功!</h4>
                <li>{{ session('success') }}</li>
            </div>
        @endif
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            {{--分页--}}
                            <form action="/admin/goods/show" method="get">
                            <div class="col-xs-2">
                                <div class="form-group">
                                    <select name="num" class="form-control">
                                        <!-- 判断num的值,并选中 -->
                                        <option
                                                @if(!empty($request['num']) && $request['num'] == 10)
                                                selected
                                                @endif
                                        >10</option>
                                        <option
                                                @if(!empty($request['num']) && $request['num'] == 20)
                                                selected
                                                @endif
                                        >20</option>
                                        <option
                                                @if(!empty($request['num']) && $request['num'] == 50)
                                                selected
                                                @endif
                                        >50</option>
                                        <option
                                                @if(!empty($request['num']) && $request['num'] == 100)
                                                selected
                                                @endif
                                        >100</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-3 col-md-offset-7">
                            <div class="input-group input-group-sm">
                                <input name="keywords" type="text" class="form-control" value="{{$request['keywords'] or ''}}">
                                <span class="input-group-btn">
                                  <button class="btn btn-info btn-flat">搜索!</button>
                                </span>
                            </div>
                        </div>
                                </form>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>商品名称</th>
                                    <th>商品价格</th>
                                    <th>销量</th>
                                    <th>推荐</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($data as $goods)

                                <tr>
                                    <td>{{ $goods -> id }}</td>
                                    <td>{{ $goods -> goods_name }}</td>
                                    <td>{{ $goods -> price}}元</td>
                                    <td ><a class="btn bg-yellow margin">{{ $goods -> sellnum }}</a></td>

                                    @if($goods -> tuijian == 0)
                                    <td id="{{$goods->id}}"><a class="btn bg-olive margin status" data-value="{{$goods -> id}}">否</a></td>
                                    @else
                                    <td id="{{$goods->id}}"><a class="btn bg-orange margin status" data-value="{{$goods -> id}}">推荐中</a></td>
                                    @endif
                                    @if($goods -> status == 1)
                                        <td><a class="btn bg-red margin">热卖中</a></td>
                                    @else
                                        <td><a class="btn bg-purple margin">下架</a></td>
                                    @endif
                                    <td><a href="/admin/goods/goodspic/{{ $goods -> id }}" class="btn bg-yellow margin"><i class="fa fa-fw fa-picture-o"></i></a><a href="/admin/goods/edit/{{ $goods -> id }}" class="btn bg-olive margin"><i class="fa fa-edit"></i></a> <a href="/admin/goods/delete/{{ $goods -> id }}" class="btn bg-maroon margin"><i class="fa fa-trash-o"></i></a></td>
                                </tr>
                                @endforeach
                                </tfoot>
                            </table>
                            <script type="text/javascript">

                                // alert($);
                                $.ajaxSetup({
                                    headers: {
                                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                                    }
                                });
                                $('.status').unbind("click").bind("click", function(){
//                                $('.status').on('click',function(){
                                    var id = $(this).attr('data-value');
                                    var This = $(this);
//                                     console.log(id);
                                    // //获取元素的父元素
                                    // var td = $(this).parent();

                                    $.ajax({
                                        type:'post',
                                        url:"{{asset('/admin/goods/ajax')}}",
                                        data:{'id':id},

                                        success:function(data)
                                        {
                                            if(data == 1)
                                            {
                                                This.removeClass('bg-olive').addClass('bg-orange').html("推荐中");
                                            }else{

                                                This.removeClass('bg-orange').addClass('bg-olive').html("否");
                                            }
                                        },
                                        dataType:'json'
                                    });
                                    // alert(111);
                                });

                            </script>
                            {!! $data->render() !!}
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->


                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->
@endsection
